package com.sens.orderback.ui.activity.alipay;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sens.alipay.Configure;
import com.sens.common.StringUtil;
import com.sens.database.LiteOrmDBUtil;
import com.sens.ipc.alipay.IPCHelper;
import com.sens.ipc.alipay.hooker.Handle;
import com.sens.network.ApiByHttp;
import com.sens.orderback.R;
import com.sens.tables.AliPayConfig;
import com.sens.tables.CommonConfig;
import com.sens.utils.ToastUtil;

public class SettingActivity extends Activity {
    private EditText editText;
    private AlertDialog.Builder hintDialog;
    private String host;

    private TextView monitorTV;
    private AlertDialog.Builder monitorDialog;
    private String monitor;
    private Handle handle;
    private CommonConfig commonConfig;
    private AliPayConfig aliPayConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay_setting);
        editText = findViewById(R.id.editText);

        commonConfig = LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).queryOne(CommonConfig.class);
        aliPayConfig = LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).queryOne(AliPayConfig.class);
        host = commonConfig.getServiceHost();
        if (host != null) editText.setText(host);

        hintDialog = new AlertDialog.Builder(this);
        hintDialog.setCancelable(false);
        hintDialog.setTitle("确定修改地址？");
        hintDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                commonConfig.setServiceHost(host);
                LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).update(commonConfig);
                ApiByHttp.getInstance().setHost(host);
                handle.setHost(host);
                com.sens.ipc.mybank.hooker.Handle h =  com.sens.ipc.mybank.IPCHelper.getHookerHandle(SettingActivity.this, null);
                h.setHost(host);
                h.unBind();
                Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK);
                finish();
            }
        });
        hintDialog.setNegativeButton("取消", null);

        monitorTV = findViewById(R.id.monitorTV);
        if (!StringUtil.isEmpty(aliPayConfig.getMonitorType())) {
            for (int i = 0; i < Configure.monitors.length; i++) {
                if (Configure.monitors[i].equals(aliPayConfig.getMonitorType())) {
                    monitor = Configure.monitorNames[i];
                }
            }
        }
        if (monitor != null) {
            monitorTV.setText(monitor);
        } else {
            monitorTV.setText("点击设置");
        }
        monitorDialog = new AlertDialog.Builder(this);
        monitorDialog.setTitle("选择一个规则");
        monitorDialog.setItems(Configure.monitorNames, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                monitor = Configure.monitorNames[which];
                monitorTV.setText(monitor);
                aliPayConfig.setMonitorType(Configure.monitors[which]);
                LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).update(aliPayConfig);
                handle.setMonitor(Configure.monitors[which]);
            }
        });
        handle = IPCHelper.getHookerHandle(this, null);
    }

    public void submit(View view) {
        host = editText.getText().toString().trim();
        if (StringUtil.isEmpty(monitor)) {
            ToastUtil.showToast("请设置采集规则");
            return;
        }
        if (StringUtil.isEmpty(host)) {
            ToastUtil.showToast("请输入正确的服务地址");
            return;
        }
        hintDialog.setMessage(monitor + "\n" + host);
        hintDialog.show();
    }

    public void close(View view) {
        finish();
    }

    public void changeMonitor(View view) {
        monitorDialog.show();
    }

    @Override
    protected void onDestroy() {
        if (handle != null) handle.unBind();
        super.onDestroy();
    }
}